package com.ih.controller;

import com.ih.pojo.MainMenu;
import com.ih.service.HistoryService;
import com.ih.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
@Slf4j
@Controller
@RequestMapping("/main")
public class MainMenuController {
    @Autowired
    private WorkerService workerService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private RedisTemplate redisTemplate;


    @RequestMapping("/mainMenu")
    @ResponseBody  //此注解表示反回的是json数据 ，而不是视图
    public List<MainMenu> mainMenu() {
        //去redis 遍历有没有这个  key
        List<MainMenu> mainMenu = redisTemplate.opsForList().range("mainMenu", 0, -1);
        List<MainMenu> arrayList = new ArrayList<>();
         if (mainMenu==null||mainMenu.size()<=0){
            //redis 没有数据
             System.out.println("redis没有数据，从数据库中查");
             List<MainMenu> list = new ArrayList<>();
             Map<Integer, Integer> map = new HashMap<>();
             //送水工的送水总数量
             historyService.queryHistory().forEach(history -> {
                 //map.getOrDefault(history.getWorkerId(),0)  表示有值则用值，没值则用默认的0
                 map.put(history.getWorkerId(), map.getOrDefault(history.getWorkerId(), 0) + history.getSendWaterCount());
             });
             //送水工人
             workerService.queryWorker().forEach(w -> {
                 MainMenu main = new MainMenu();
                 main.setWorkerName(w.getWorkerName());
                 Integer count = map.get(w.getWid());
                 main.setSendWaterCount(count == null ? 0 : count);
                 list.add(main);
             });

            mainMenu.addAll(list);

            //另一种排序方法
             Collections.sort(mainMenu,((o1, o2) -> {
                 if (o2.getSendWaterCount()>o1.getSendWaterCount()){
                     return 1;
                 }else if (o2.getSendWaterCount()< o1.getSendWaterCount()){
                     return -1;
                 }else {
                     return 0;
                 }
             }));

             if (mainMenu.size() < 12) {
                 arrayList.addAll(mainMenu);
             }


             for (int i = 0; i < 12; i++) {
                 arrayList.add(mainMenu.get(i));
             }
             arrayList.forEach(System.out::println);
             Collections.reverse(arrayList);
             //把数据库查询到的数据 设置到redis
             redisTemplate.opsForList().leftPushAll("mainMenu",arrayList);
         }else {
             System.out.println("从redis中获取数据");
             arrayList.addAll(mainMenu);
         }


        //根据送水数量排序
       /* Comparator com = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof MainMenu && o2 instanceof MainMenu) {
                    MainMenu main1 = (MainMenu) o1;
                    MainMenu main2 = (MainMenu) o2;
                    //倒序
                    return Integer.compare(main2.getSendWaterCount(), main1.getSendWaterCount());
                } else {
                    throw new RuntimeException("输入类型不匹配！");
                }
            }
        };
        Collections.sort(mainMenu, com);*/


        System.out.println("arrayList = " + arrayList);
        return arrayList;
    }
}
